﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using MPP.Product.DataEntity;

namespace MPP.Product.DataManage
{
    public class FunctionItemManage
    {
        /// <summary>
        /// Inserts the function item.
        /// </summary>
        /// <param name="functionItem">The function item.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public FunctionItemDE InsertFunctionItem(FunctionItemDE functionItem)
        {
            string strConn = SqlHelper.OracleMainDBConnectionString;
            using (OracleConnection oracleConn = new OracleConnection(strConn))
            {
                oracleConn.Open();

                int id = SqlHelper.GetNextID("FunctionItem", "FUNCTIONITEMID");

                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into FUNCTIONITEM(FUNCTIONITEMID,MOBILEPHONEPRODUCTID,FUNCTIONITEMTYPEID,DETAIL, CREATEDTIME ,LASTMODIFIEDTIME ,ISDELETED ,VERSION)");
                strSql.Append(" values (@FUNCTIONITEMID, @MOBILEPHONEPRODUCTID,@FUNCTIONITEMTYPEID,@DETAIL,@CREATEDTIME,@LASTMODIFIEDTIME,@ISDELETED,@VERSION)");


                OracleParameter[] parameters = new OracleParameter[]
               {
                  new OracleParameter("@FUNCTIONITEMID",OracleType.Int32,40),
                  new OracleParameter("@MOBILEPHONEPRODUCTID",OracleType.Int32,40),
                  new OracleParameter("@FUNCTIONITEMTYPEID",OracleType.Int32,40),
                  new OracleParameter("@DETAIL",OracleType.VarChar,500),
                  new OracleParameter("@CREATEDTIME",OracleType.DateTime,40),
                  new OracleParameter("@LASTMODIFIEDTIME",OracleType.DateTime,40),
                  new OracleParameter("@ISDELETED",OracleType.Char,1),
                  new OracleParameter("@VERSION",OracleType.VarChar,40),
                 };
                parameters[0].Value = id;
                parameters[1].Value = functionItem.MobilePhoneProductID;
                parameters[2].Value = functionItem.FunctionItemTypeID;
                parameters[3].Value = functionItem.Detail;
                parameters[4].Value = functionItem.CreatedTime;
                parameters[5].Value = functionItem.LastModifiedTime;
                parameters[6].Value = functionItem.IsDeleted;
                parameters[7].Value = functionItem.Version;

                OracleCommand oracleCommand = new OracleCommand(strSql.ToString(), strConn);
                int returndata = oracleCommand.ExecuteNonQuery();
                if (returndata > 0)
                {
                    StringBuilder strSqlSelect = new StringBuilder();
                    strSqlSelect.Append("select * from FUNCTIONITEMTYPE where FUNCTIONITEMTYPEID=@FUNCTIONITEMTYPEID");

                    OracleDataAdapter oracleAdapter = new OracleDataAdapter(strSqlSelect.ToString(), oracleConn);
                    DataTable datatable = new DataTable();
                    oracleAdapter.Fill(datatable);
                    oracleConn.Close();

                    FunctionItemDE newFunctionItem = new FunctionItemDE();
                    if (datatable.Rows.Count > 0)
                    {
                        newFunctionItem.FunctionItemID = Convert.ToInt32(datatable.Rows[0][0]);
                        newFunctionItem.FunctionItemTypeID = Convert.ToInt32(datatable.Rows[0][1]);
                        newFunctionItem.MobilePhoneProductID = Convert.ToInt32(datatable.Rows[0][2]);
                        newFunctionItem.Detail = datatable.Rows[0][3].ToString();
                        newFunctionItem.CreatedTime = Convert.ToDateTime(datatable.Rows[0][4]);
                        newFunctionItem.LastModifiedTime = Convert.ToDateTime(datatable.Rows[0][5]);
                        newFunctionItem.IsDeleted = Convert.ToBoolean(datatable.Rows[0][6]);
                        newFunctionItem.Version = Convert.ToInt32(datatable.Rows[0][7]);
                    }

                    return newFunctionItem;
                }
            }
        }

    }
}
